package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.*;
import com.hmall.model.MallProductPlan;
import com.hmall.model.SysCustomer;
import com.hdmall.business.common.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class PlanManageService {
    private final
    SysCustomerMapper sysCustomerMapper;
    private final
    MallProductPlanMapper mallProductPlanMapper;
    private final
    SysUserMapper sysUserMapper;

    @Autowired
    public PlanManageService(SysCustomerMapper sysCustomerMapper, MallProductPlanMapper mallProductPlanMapper, SysUserMapper sysUserMapper, MallProductPlanItemMapper mallProductPlanItemMapper, MallProductMapper mallProductMapper) {
        this.sysCustomerMapper = sysCustomerMapper;
        this.mallProductPlanMapper = mallProductPlanMapper;
        this.sysUserMapper = sysUserMapper;
    }

    public void createPlan(Integer customer_id, Date create_time) {
        //获得该客户最早注册的用户
        Integer user_id = sysCustomerMapper.selectCustomerUserId(customer_id);
        if (user_id == null) {
            ServerResponse.createByErrorMessage("该客户下没有用户");
            return;
        }

        SysCustomer sysCustomer = sysCustomerMapper.selectByPrimaryKey(customer_id);
        //获得该客户绑定的业务员
        Integer sale_user_id = sysCustomer.getSalesmanId();
        //获得办事处id
        Integer dealer_id = sysCustomer.getDealerId();
        //生成计划编号
//        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
        Long plan_no = NumberUtil.createPlanNo(dealer_id);

        Integer status = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
        if (create_time.before(calendar.getTime())){
            status = 4;
        }
        //新建计划
        MallProductPlan mallProductPlan = new MallProductPlan();
        mallProductPlan.setUserId(user_id);
        mallProductPlan.setSaleUserId(sale_user_id);
        mallProductPlan.setCreateTime(create_time);
        mallProductPlan.setStatus(status);
        mallProductPlan.setDealerId(dealer_id);
        mallProductPlan.setSubmitPerson(1);
        mallProductPlan.setPlanNo(plan_no);

        mallProductPlanMapper.insert(mallProductPlan);
        MallProductPlan mallProductPlan1 = mallProductPlanMapper.selectByNo(plan_no);
        Map<String, String> map = new HashMap<>();
        // map.put("address", shippingId.toString());
        map.put("id", mallProductPlan1.getPlanId().toString());
        map.put("deal_way", mallProductPlan1.getDealWay());
        map.put("remark", mallProductPlan1.getRemark());

        map.put("create_time", mallProductPlan1.getCreateTime().toString());
        String statusStr = "";
        switch (mallProductPlan1.getStatus()) {
            case 0: {
                statusStr = "未审核";
                break;
            }
            case 1: {
                statusStr = "审核中";
                break;
            }
            case 2: {
                statusStr = "通过";
                break;
            }
            case 3: {
                statusStr = "未通过";
                break;
            }
        }
        map.put("status", statusStr);
        String salesman_name = sysUserMapper.selectByPrimaryKey(mallProductPlan.getSaleUserId()).getUsername();
        map.put("salesman_name", salesman_name);
        map.put("plan_no", mallProductPlan1.getPlanNo().toString());
        map.put("opinion", mallProductPlan1.getOpinion());

        String username = sysUserMapper.selectByPrimaryKey(mallProductPlan.getUserId()).getUsername();
        map.put("username", username);
        map.put("productList", "");
//        String s = HttpClientUtil.doPostWithParam("http://47.98.171.71:8081/hdfind/createPlan", map);
        //生成索引
//        Map map = preIndex(mallProductPlan);
//        String s = HttpClientUtil.doPostWithParam("http://47.98.171.71:8081/hdfind/createPlan", map);

        ServerResponse.createBySuccess("创建成功");
    }
//
//    private Map preIndex(MallProductPlan mallProductPlan) {
//
//
//        Map map = new HashMap();
//        // map.put("address", shippingId.toString());
//        map.put("id", mallProductPlan.getPlanId().toString());
//        map.put("deal_way", mallProductPlan.getDealWay());
//        map.put("remark", mallProductPlan.getRemark());
//
//        map.put("create_time", mallProductPlan.getCreateTime().toString());
//        String statusStr = "";
//        switch (mallProductPlan.getStatus()) {
//            case 0: {
//                statusStr = "未审核";
//                break;
//            }
//            case 1: {
//                statusStr = "审核中";
//                break;
//            }
//            case 2: {
//                statusStr = "通过";
//                break;
//            }
//            case 3: {
//                statusStr = "未通过";
//                break;
//            }
//        }
//        map.put("status", statusStr);
//        String salesman_name = sysUserMapper.selectByPrimaryKey(mallProductPlan.getSaleUserId()).getUsername();
//        map.put("salesman_name", salesman_name);
//        map.put("plan_no", mallProductPlan.getPlanNo().toString());
//        map.put("opinion", mallProductPlan.getOpinion());
//
//        String username = sysUserMapper.selectByPrimaryKey(mallProductPlan.getUserId()).getUsername();
//        map.put("username", username);
//        List<MallProductPlanItemVo> mallProductPlanItemVoList = new ArrayList<>();
//        List<MallProductPlanItemVo> mallProductPlanItemList = mallProductPlanItemMapper.selectByPlanId(mallProductPlan.getPlanNo());
//        for (MallProductPlanItem mallProductPlanItem : mallProductPlanItemList) {
//            int productId = mallProductPlanItem.getProductId();
//            MallProduct mallProduct = mallProductMapper.selectByPrimaryKey(productId);
//            mallProductPlanItemVoList.add(new MallProductPlanItemVo(mallProductPlanItem.getAmount(), mallProductPlanItem.getGrade(), mallProduct.getName()));
//        }
//        map.put("productList", mallProductPlanItemVoList.toString());
//        return map;
//    }
}
